package com.sso.utils;

import com.sso.filter.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * 上传文件工具类
 *
 * @author HP
 */
@Slf4j(topic = "FileUtil")
public class FileUtil {

	public static R uploadFile(MultipartFile files, String location) {
		log.info("开始上传文件");
		// 获取文件名
		String newFileName;
		String url;
		R r = R.ok();
			try {
				String fileName = files.getOriginalFilename();
				assert fileName != null;
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				newFileName = "SE2" + suffix;
				log.info("上传文件文件名称：{}", newFileName);
				log.info("上传文件大小 ：{}", files.getSize());
				log.info(location);
				File file1 = new File(location, newFileName);
				log.info("文件路径{}", file1.getAbsolutePath());
				FileUtils.copyInputStreamToFile(files.getInputStream(), file1);
				url = location+newFileName;
			} catch (IOException e) {
				e.printStackTrace();
				log.info("文件上传失败！");
				return R.error("文件上传失败，请重试");
			}
			r.put("url", url);
		log.info("图片上传成功,{}", r);
		return r;
	}

	public static R uploadFile2(MultipartFile[] files, String location) {
		log.info("开始上传文件");
		// 获取文件名
		String newFileName;
		String url;
		R r = R.ok();
		for (MultipartFile file : files) {
			try {
				String fileName = file.getOriginalFilename();
				assert fileName != null;
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				newFileName = UUID.randomUUID().toString() + suffix;
				log.info("上传文件文件名称：{}", newFileName);
				log.info("上传文件大小 ：{}", file.getSize());
				log.info(location);
				File file1 = new File(location+"shale_porosity/data/FibSem/", newFileName);
				log.info("文件路径{}", file1.getAbsolutePath());
				FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
//				url = file1.getAbsolutePath().replace(UploadEnum.Path.getPath(),"");
				url = location+"shale_porosity/data/FibSem/"+newFileName;
			} catch (IOException e) {
				e.printStackTrace();
				log.info("文件上传失败！");
				return R.error("文件上传失败，请重试");
			}
			r.put("url", url);
		}
		log.info("图片上传成功,{}", r);
		return r;
	}

	public static R uploadFile1(MultipartFile[] files, String location) {
		log.info("开始上传文件");
		// 获取文件名
		String newFileName;
		String url;
		R r = R.ok();
		for (MultipartFile file : files) {
			try {
				String fileName = file.getOriginalFilename();
				assert fileName != null;
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				newFileName = UUID.randomUUID().toString() + suffix;
				log.info("上传文件文件名称：{}", fileName);
				log.info("上传文件大小 ：{}", file.getSize());
				log.info(location);
				File file1 = new File(location+"shale_porosity/data/QemScan/", newFileName);
				log.info("文件路径{}", file1.getAbsolutePath());
				FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
//				url = file1.getAbsolutePath().replace(UploadEnum.Path.getPath(),"");
				url = location +"shale_porosity/data/QemScan/"+ newFileName;
			} catch (IOException e) {
				e.printStackTrace();
				log.info("文件上传失败！");
				return R.error("文件上传失败，请重试");
			}
			r.put("url", url);
		}
		log.info("图片上传成功,{}", r);
		return r;
	}


	/**
	 *  递归删除文件或者文件夹
	 */
	public static void deleteFiles(File file) {
		if (!file.exists()) {
			return;
		}
		if (file.isFile()) {
			// 文件: 直接删除
			file.delete();
		} else if (file.isDirectory()) {
			// 文件夹
			// 1. 删除子文件
			for (File f : Objects.requireNonNull(file.listFiles())) {
				deleteFiles(f);
			}
			// 2. 删除文件夹
			file.delete();
		}
	}



	public static String getFileType(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		assert fileName != null;
		return fileName.substring(fileName.lastIndexOf("."));
	}
}
